package cn.biplam.front.template.base;

import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Clear_Type;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.template.base.Button.Button_Type;

/**
 * 有提交按钮的Popup弹出窗口模版<br>
 * <ul>功能：
 * 	 <li> 弹出窗口布局
 *  <li> 弹出窗口放入提交和取消按钮
 * </ul>
 * 
 * @author XiaoRuxing 2009-7-4下午04:47:27
 * @version 1.0
 */
/**
 * 
 * @author XiaoRuxing 2009-7-4下午06:40:31
 * @version 1.0
 */
public class SubmitPopup {

	private String ID;
	private String recordId;
	/** 系统路径 */
	@SuppressWarnings("unused")
	private String sHomeUrl;
	/** 用户信息 */
	private UserInfo mUser;
	/** 目标弹出窗口容器ID*/
	private String targetID;
	/** 提交脚本命令*/
	private String submitScript;
	/** 按钮组 */
	private ButtonGroup buttons;
	/** 主信息界面*/
	private HtmlWindow domain;
	/** 弹出窗口内容 */
	private HtmlWindow window;

	/** 弹出窗口标题 */
	private String title;

	/** 占位宽度 */
	private String sWidth;
	/** 占位高度 */
	private String sHeight;
	/** 界面按钮个数 */
	private int buttonCount = 0;
	/** 点击提交后是否关闭窗口 */
	private boolean isClosePopup;

	/**
	 * 创建弹出窗口模版
	 * @param ID			页面ID
	 * @param sHomeUrl		系统路径
	 * @param mUser			用户信息
	 * @param targetID		目标容器ID(弹出窗口)
	 * @param recordId      记录ID
	 * @param submitScript	提交脚本命令
	 * @throws ErrorMessage 
	 */
	public SubmitPopup(String ID,String sHomeUrl,
			UserInfo mUser,String targetID,
			String submitScript,String recordId) throws ErrorMessage{
		this(ID,sHomeUrl,mUser,targetID,submitScript,recordId,true);
	}

	/**
	 * 创建弹出窗口模版
	 * @param ID			页面ID
	 * @param sHomeUrl		系统路径
	 * @param mUser			用户信息
	 * @param targetID		目标容器ID(弹出窗口)
	 * @param recordId      记录ID
	 * @param submitScript	提交脚本命令
	 * @param isClosePopup 点击确定按钮提交后是否关闭窗口
	 * @throws ErrorMessage 
	 */
	public SubmitPopup(String ID,String sHomeUrl,
			UserInfo mUser,String targetID,
			String submitScript,String recordId,
			boolean isClosePopup) throws ErrorMessage{
		this.ID = ID;
		this.sHomeUrl = sHomeUrl;
		this.mUser = mUser;
		this.targetID = targetID;
		this.submitScript = submitScript;
		this.recordId = recordId;
		this.window = new HtmlDiv(ID);
		this.buttons = new ButtonGroup(null,Float_Type.Left);
		this.isClosePopup = isClosePopup;
	}

	/**
	 * 设置页面占位大小
	 * @param sWidth 宽度
	 * @param sHeight 高度 
	 * @throws ErrorMessage 
	 */
	public void setSize(String sWidth,
			String sHeight) throws ErrorMessage{
		this.sHeight = sHeight;
		this.sWidth = sWidth;
		if(this.sHeight!=null){
			this.window.setStyleHeight(sHeight);
		}
		if(this.sWidth!=null){
			this.window.setStyleWidth(sWidth);
		}
	}

	/**
	 * 设置弹出窗口标题
	 * @param title 标题
	 */
	public void setTitle(String title){
		this.title = title;
	}

	/**
	 * 获取提交按钮脚本
	 * @return 如果设置提交后关闭窗口加入关闭窗口脚本
	 */
	private String _getSubmitScript(){
		if(this.isClosePopup){
			return PopupScript.submit(this.targetID, this.submitScript);
		}else{
			return this.submitScript;
		}
	}

	/**
	 * 放入默认的确定和取消按钮
	 * @throws ErrorMessage
	 */
	private void _putButton() throws ErrorMessage{
		this.putButton(null, SysCaption.Submit, Button_Type.Click, 
				_getSubmitScript());
		this.putButton(null, SysCaption.Return, Button_Type.Click, 
				PopupScript.cancel(this.targetID));
	}

	/**
	 * 向模板中放入按钮(按自右向左顺序放入)
	 * @param buttonID	按钮ID
	 * @param captionID	标题名称ID	
	 * @param type		按钮类型	只读/可点击
	 * @param onClick	点击脚本命令
	 * @throws ErrorMessage 
	 */
	public void putButton(String buttonID,String captionID,Button_Type type,String onClick) throws ErrorMessage{
		this.buttons.put(buttonID, CaptionComp.get(this.mUser, captionID), type, onClick);
		buttonCount++;
	}

	/**
	 * 向模板信息区放入HtmlWindow对象
	 * @param domain
	 * @throws ErrorMessage
	 */
	public void putDomain(HtmlWindow domain) throws ErrorMessage{
		this.domain = domain;
	}

	/**
	 * 放入当前控件的记录ID 
	 * @throws ErrorMessage
	 */
	private void _putKeyValue() throws ErrorMessage{
		this.window.put(new HtmlValue(this.ID+"r",this.ID+"r",recordId));
	}

	/**
	 * 放入控件,用于传参
	 * @param extent 参数
	 * @throws ErrorMessage
	 */
	public void put(HtmlWindow extent) throws ErrorMessage{
		this.window.put(extent);
	}

	/**
	 * 放入控件,用于传参
	 * @param extent 参数
	 * @throws ErrorMessage
	 */
	public void put(HtmlValue extent) throws ErrorMessage{
		this.window.put(extent);
	}

	/**
	 * 计算按钮离组件左边距离 
	 * @return
	 */
	private String _generatorLeftDistance(){
		int width = TOOLS.stringToInt(this.sWidth);
		int leftWidth = width/2-this.buttons.getLength()/2;
		if(leftWidth>0){
			return TOOLS.intToString(leftWidth);
		}else{
			return null;
		}
	}

	/**
	 * 获取弹出窗口界面
	 * @return
	 * @throws ErrorMessage
	 */
	public HtmlWindow get() throws ErrorMessage{
		this._putKeyValue();
		this.domain.setMarginLeft("5");
		this.window.put(this.domain);
		if(this.targetID!=null){
			this._putButton();
			if(this.title!=null){
				this.window.bind("$('"+this.targetID+"ft').innerText='"+this.title+"';");
			}
			HtmlWindow bt=buttons.get();
			bt.setStyleHeight("25");
			bt.setMarginLeft(_generatorLeftDistance());
			bt.setMarginTop("15");
			bt.setClear(Clear_Type.Both);
			this.window.put(bt);
		}
		return this.window;
	}
}
